package com.flyone.fuxiao.utils;

import com.flyone.fuxiao.constant.FileConstant;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Base64;

/**
 * 解码工具
 *
 * @author flyone
 * @date 2024/3/23
 */
@Component
public class DecodeUtils {

    @Resource
    private QiniuUtils qiniuUtils;

    public String getImage(String base64Data, String filePath) {
        // 解码Base64数据
        byte[] imageBytes = Base64.getDecoder().decode(base64Data);
        String uuid = RandomStringUtils.randomAlphanumeric(8);
        String filename = uuid + "-" + filePath + ".png";
        boolean result = qiniuUtils.uploadByBytes(imageBytes, filename);
        if (result) {
            // 返回可访问地址
            return FileConstant.QINIU_HOST + filename;
        }
        return "上传失败";
    }
}
